package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jDingTalk;
import com.yuanheng100.ems4j.model.Ems4jPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class Ems4jDingTalkSqlProvider
{

    public String selectCountByCondition(@Param("conditions") final Map<String, Object> conditions)
    {
        return new SQL()
        {
            {
                SELECT("COUNT(*)");
                FROM("ems4j_dingtalk");
                if (conditions.get("msgtype") != null)
                {
                    WHERE("msgtype = #{msgtype,jdbcType=VARCHAR}");
                }
                if (conditions.get("sender") != null)
                {
                    WHERE("sender = #{sender,jdbcType=VARCHAR}");
                }
                if (conditions.get("touser") != null)
                {
                    WHERE("touser = #{touser,jdbcType=VARCHAR}");
                }

                if (conditions.get("toparty") != null)
                {
                    WHERE("toparty = #{toparty,jdbcType=VARCHAR}");
                }
                if (conditions.get("resultCode") != null)
                {
                    WHERE("resultCode = #{resultCode,jdbcType=SMALLINT}");
                }
            }
        }.toString();
    }

    public String selectListByCondition(@Param("conditions") final Map<String, Object> conditions, @Param("page") final Ems4jPage<Ems4jDingTalk> page)
    {
        return new SQL()
        {
            {
                SELECT("*");
                FROM("ems4j_dingtalk");
                if (conditions.get("msgtype") != null)
                {
                    WHERE("msgtype = #{conditions.msgtype,jdbcType=VARCHAR}");
                }
                if (conditions.get("sender") != null)
                {
                    WHERE("sender = #{conditions.sender,jdbcType=VARCHAR}");
                }
                if (conditions.get("touser") != null)
                {
                    WHERE("touser = #{conditions.touser,jdbcType=VARCHAR}");
                }

                if (conditions.get("toparty") != null)
                {
                    WHERE("toparty = #{conditions.toparty,jdbcType=VARCHAR}");
                }
                if (conditions.get("resultCode") != null)
                {
                    WHERE("resultCode = #{conditions.resultCode,jdbcType=SMALLINT}");
                }
                if (page.getOrderBy() != null)
                {
                    ORDER_BY(page.getOrderBy() + " " + (page.getOrder() == null ? "ASC" : page.getOrder()));
                }
            }
        }.toString() + " LIMIT " + (page.getPageNo() - 1) * page.getPageSize() + "," + page.getPageSize();
    }

    public String insertSelective(Ems4jDingTalk record)
    {
        BEGIN();
        INSERT_INTO("ems4j_dingtalk");

        if (record.getDingId() != null)
        {
            VALUES("dingId", "#{dingId,jdbcType=INTEGER}");
        }

        if (record.getMsgtype() != null)
        {
            VALUES("msgtype", "#{msgtype,jdbcType=VARCHAR}");
        }

        if (record.getSender() != null)
        {
            VALUES("sender", "#{sender,jdbcType=VARCHAR}");
        }

        if (record.getCid() != null)
        {
            VALUES("cid", "#{cid,jdbcType=VARCHAR}");
        }

        if (record.getContent() != null)
        {
            VALUES("content", "#{content,jdbcType=VARCHAR}");
        }

        if (record.getResultCode() != null)
        {
            VALUES("resultCode", "#{resultCode,jdbcType=SMALLINT}");
        }

        if (record.getSendTime() != null)
        {
            VALUES("sendTime", "#{sendTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTitle() != null)
        {
            VALUES("title", "#{title,jdbcType=VARCHAR}");
        }

        if (record.getTouser() != null)
        {
            VALUES("touser", "#{touser,jdbcType=VARCHAR}");
        }

        if (record.getToparty() != null)
        {
            VALUES("toparty", "#{toparty,jdbcType=VARCHAR}");
        }

        if (record.getMedia_id() != null)
        {
            VALUES("media_id", "#{media_id,jdbcType=INTEGER}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(Ems4jDingTalk record)
    {
        BEGIN();
        UPDATE("ems4j_dingtalk");

        if (record.getMsgtype() != null)
        {
            SET("msgtype = #{msgtype,jdbcType=VARCHAR}");
        }

        if (record.getSender() != null)
        {
            SET("sender = #{sender,jdbcType=VARCHAR}");
        }

        if (record.getCid() != null)
        {
            SET("cid = #{cid,jdbcType=VARCHAR}");
        }

        if (record.getContent() != null)
        {
            SET("content = #{content,jdbcType=VARCHAR}");
        }

        if (record.getResultCode() != null)
        {
            SET("resultCode = #{resultCode,jdbcType=SMALLINT}");
        }

        if (record.getSendTime() != null)
        {
            SET("sendTime = #{sendTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTitle() != null)
        {
            SET("title = #{title,jdbcType=VARCHAR}");
        }

        if (record.getTouser() != null)
        {
            SET("touser = #{touser,jdbcType=VARCHAR}");
        }

        if (record.getToparty() != null)
        {
            SET("toparty = #{toparty,jdbcType=VARCHAR}");
        }

        if (record.getMedia_id() != null)
        {
            SET("media_id = #{media_id,jdbcType=INTEGER}");
        }

        WHERE("dingId = #{dingId,jdbcType=INTEGER}");

        return SQL();
    }
}